let addItemApp=new Vue({
    el:"#addItemApp",
    data:{
        categories:[],
        itemsName:"",
        catId:"",
        itemsSynopsis:"",
        unitPrice:"",
        specification:"",
        stock:""
    },
    methods: {
        loadItemsCate:function () {
            axios({
                url: "/categories",
                method:"get"
            }).then(function (response) {
                addItemApp.categories=response.data
            })
        },
        select:function (catId,catName) {

            $("#dropdownMenu1").text(catName);
            addItemApp.catId=catId;
        },
        addItem:function () {
            let files=document.getElementById("imageFile").files;
            if (files.length > 0){
                addItemApp.itemsImage=files[0]
            }else {
                alert("请选择主图")
                return;
            }
            let imageUrl=$("#image").attr("src")
            for (let i = 0; i < 3; i++) {
                imageUrl=imageUrl.slice(imageUrl.indexOf("/")+1);
            }
            let url=imageUrl.substring(0);
            let content = $('#summernote').val();
            let statusName=$("#dropdownMenu2").text();
            let status;
            if (statusName ==="在售"){
                status=1;
            }else {
                status=2;
            }

            let form=new FormData;
            form.append("itemsName",this.itemsName);
            form.append("catId",this.catId);
            form.append("itemsImage",url);
            form.append("itemsSynopsis",this.itemsSynopsis);
            form.append("unitPrice",this.unitPrice);
            form.append("specification",this.specification);
            form.append("stock",this.stock);
            form.append("itemsStatus",status);
            form.append("itemsContent",content);
            console.log(form)
            axios({
                url:"/backItems/add",
                method:"post",
                data:form
            }).then(function (response) {
                if (response.data === "ok"){
                    alert("商品新增成功，确认跳转商品列表")
                    location.href="/back/index.html"
                }else {
                    alert(response.data)
                }
            })
        },
    },
    created:function () {
        this.loadItemsCate()
    }
})